'''
Created on Oct 15, 2012

@author: raygomez
'''
import unittest
import binascii
import xdrlib
from Message import Message
from ProtocolConstants import *



class Test(unittest.TestCase):


    def testDWRMandatory(self):
        raw = binascii.a2b_hex("01000024800001180000000000000000000000000"
                                + "0000108000000080000012800000008")
        unpacker = xdrlib.Unpacker(raw)
        dwr = Message()
        self.assertEqual(dwr.decode(unpacker, len(raw)), Message.DECODE_STATUS_DECODED)
        
        self.assertEqual(1, dwr.hdr.version)
        self.assertEqual(DIAMETER_COMMAND_DEVICE_WATCHDOG, dwr.hdr.commandCode)
        self.assertEqual(0, dwr.hdr.applicationID)
        self.assertEqual(0, dwr.hdr.hopByHopIdentifier)
        self.assertEqual(0, dwr.hdr.endToEndIdentifier)
        
        originHost = dwr.find(DI_ORIGIN_HOST)
        self.assertIsNotNone(originHost)
        self.assertEqual(b'', originHost.payload)
        
        originRealm = dwr.find(DI_ORIGIN_REALM)
        self.assertIsNotNone(originRealm)
        self.assertEqual(b'', originRealm.payload)

        originState = dwr.find(DI_ORIGIN_STATE_ID)
        self.assertIsNone(originState)

    def testDWRAllFields(self):
        raw = binascii.a2b_hex("010000588000011800000000000000000000000" 
                               + "0000001080000001A6F726967696E686F73742E6D6D652E636F6D00000000012"
                               + "80000001A6F726967696E7265616C6D6E736E2E636F6D0000000001160000000C00000001")
        unpacker = xdrlib.Unpacker(raw)
        dwr = Message()
        self.assertEqual(dwr.decode(unpacker, len(raw)), Message.DECODE_STATUS_DECODED)
        
        self.assertEqual(1, dwr.hdr.version)
        self.assertEqual(DIAMETER_COMMAND_DEVICE_WATCHDOG, dwr.hdr.commandCode)
        self.assertEqual(0, dwr.hdr.applicationID)
        self.assertEqual(0, dwr.hdr.hopByHopIdentifier)
        self.assertEqual(0, dwr.hdr.endToEndIdentifier)
        
        originHost = dwr.find(DI_ORIGIN_HOST)
        self.assertIsNotNone(originHost)
        self.assertEqual(b"originhost.mme.com", originHost.payload)
        
        originRealm = dwr.find(DI_ORIGIN_REALM)
        self.assertIsNotNone(originRealm)
        self.assertEqual(b"originrealmnsn.com", originRealm.payload)

        originState = dwr.find(DI_ORIGIN_STATE_ID)
        self.assertIsNotNone(originState)
        self.assertEqual(b'\x00\x00\x00\x01', originState.payload)
        
    def testPrint(self):
        raw = binascii.a2b_hex("010000588000011800000000000000000000000" 
                               + "0000001080000001A6F726967696E686F73742E6D6D652E636F6D00000000012"
                               + "80000001A6F726967696E7265616C6D6E736E2E636F6D0000000001160000000C00000001")
        unpacker = xdrlib.Unpacker(raw)
        dwr = Message()
        dwr.decode(unpacker, len(raw))
        self.assertIsNotNone(dwr)
        print(dwr)
        
if __name__ == "__main__":
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
